<template>
    <show-modal :title="title" :loading="loading" width="500px" @cancel="cancel" @submit="submit">
        <a-form-model ref="form" :model="form" :rules="rules" v-bind="layout">
            <!-- <a-form-model-item label="公司LOGO" prop="companyName">
                <upload-avatar />
            </a-form-model-item> -->
            <a-form-model-item label="公司名称" prop="companyName">
                <a-input v-model="form.companyName" placeholder="请输入公司名称" />
            </a-form-model-item>
            <a-form-model-item label="公司编码" prop="companyCode">
                <a-input v-model="form.companyCode" :disabled="form.tenantId?true:false" :maxLength="20" placeholder="请输入公司编码" />
            </a-form-model-item>
            <a-form-model-item label="公司地址" prop="companyAddress">
                <a-input v-model="form.companyAddress" placeholder="请输入公司地址" />
            </a-form-model-item>
            <a-form-model-item label="联系电话" prop="telephone">
                <a-input v-model="form.telephone" placeholder="请输入联系电话" />
            </a-form-model-item>
            <a-form-model-item label="公司账号" prop="tenantName">
                <a-input v-model="form.tenantName" :disabled="form.tenantId?true:false" placeholder="请输入公司账号" />
            </a-form-model-item>
            <a-form-model-item label="初始密码" prop="tenantPassword" v-if="!form.tenantId">
                <a-input v-model="form.tenantPassword" type="text" disabled placeholder="请输入登录密码" />
            </a-form-model-item>
            <a-form-model-item label="状态">
                <a-switch :checked="form.status == 1 ? true : false" @change="onChangeShow" />
            </a-form-model-item>
        </a-form-model>
    </show-modal>
</template>

<script>
import { rules } from './rules';
import { addInfo, updateInfo } from '@/api/system/tenant';
import UploadAvatar from '@/components/uploadAvatar'

export default {
    name: 'CompanyInfo',
    components:{
        UploadAvatar
    },
    props: {
        info: {
            type: Object,
            default: () => {}
        }
    },
    data() {
        return {
            loading: false,
            title: '公司信息',
            rules,
            form: {
                companyName: '',
                companyCode: '',
                companyAddress: '',
                telephone: '',
                tenantName: '',
                tenantPassword: '123456',
                status: 1
            },
            layout: {
                labelCol: { span: 5 },
                wrapperCol: { span: 19 }
            }
        };
    },
    created() {
        this.form = { ...this.form, ...this.info };
    },
    methods: {
        submit() {
            this.$refs.form.validate(async valid => {
                if (valid) {
                    try {
                        this.loading = true;
                        let res = null;
                        if (this.form.tenantId) {
                            //编辑
                            this.form.tenantPassword='';
                            res = await updateInfo(this.form);
                        } else {
                            //新增
                            res = await addInfo(this.form);
                        }
                        if (res.code == 200) {
                            this.$message.success('操作成功!');
                            this.$emit('submit');
                        } else {
                            this.$message.error(res.message);
                        }
                    } finally {
                        this.loading = false;
                    }
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        //切换状态
        onChangeShow(checked) {
            if (checked) {
                this.form.status = 1;
            } else {
                this.form.status = 0;
            }
        },
        //取消
        cancel() {
            this.$emit('cancel');
        }
    }
};
</script>

<style lang="scss" scoped></style>
